//
//  ShowMenuListView.m
//  EMMApp
//
//  Created by Mac on 16/5/10.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "ShowMenuListView.h"
#import "MenuListCell.h"

@interface ShowMenuListView ()<UITableViewDelegate,UITableViewDataSource> {
    NSInteger current;
    NSInteger currentSub;
    NSInteger currentSubOne;

    NSInteger c1;
    NSInteger c2;
    NSInteger c3;
    NSInteger gaoduH;
}


@property (nonatomic, copy) NSArray *numArr;
@property (nonatomic, copy) NSArray *numOneArr;
@property (nonatomic, copy) NSArray *numListArr;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UITableView *subTableView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *titleLabelSub;


@property (nonatomic, strong) NSString *nameSyting;

@end

@implementation ShowMenuListView

- (id)initWithFrame:(CGRect)frame title:(NSString *)title titleNmae:(NSString *)titleNmae numLits:(NSArray *)numList nameArr:(NSArray *)nameArr items:(NSArray *)items count:(NSInteger)count countSub:(NSInteger)countSub countSubOne:(NSInteger)countSubOne gaodu:(NSInteger)gaodu {
    if (self == [super initWithFrame:frame]) {
        self.numArr = items;
        gaoduH = gaodu;
        self.numOneArr = nameArr;
        self.numListArr = numList;
        _nameSyting = titleNmae;
        
        current = count;
        currentSub = countSub;
        currentSubOne = countSubOne;
        c1 = count;
        c2 = countSub;
        c3 = countSubOne;
        [self showUI];
    }
    return self;
}

- (void)showUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [self addSubview:view];
    
    //
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [view addGestureRecognizer:tap];
    
    //
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, gaoduH)];
    subView.backgroundColor = [UIColor whiteColor];
    subView.userInteractionEnabled = YES;
    [self addSubview:subView];
    
    //
    if ([_nameSyting isEqualToString:@"1"] &&![_nameSyting isEqualToString:@""]) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, subView.frame.size.width, subView.frame.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [subView addSubview:_tableView];
    }else {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, subView.frame.size.width/2, subView.frame.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorFromHexCode:@"#e4e4e4"];
        [subView addSubview:_tableView];
        
        _subTableView= [[UITableView alloc] initWithFrame:CGRectMake(subView.frame.size.width/2, 0, subView.frame.size.width/2, subView.frame.size.height) style:UITableViewStylePlain];
        _subTableView.delegate = self;
        _subTableView.dataSource = self;
        _subTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [subView addSubview:_subTableView];
    }
    //
    
}

- (void)tapClick:(UITapGestureRecognizer *)tap {
    if ([_nameSyting isEqualToString:@"1"]) {
        if (_touchBlock) {
            _touchBlock(@"tap",@"",@"");
        }
    }else {
        if (_touchBlock) {
            _touchBlock(@"",@"tap1",@"tap2");
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([_nameSyting isEqualToString:@"2"]) {
        if (tableView == _tableView) {
            return self.numOneArr.count;
        }else if (tableView == _subTableView) {
            NSArray *arr = [_numListArr objectAtIndex:currentSub];
            
            return arr.count;
        }
    }
    return self.numArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_nameSyting isEqualToString:@"2"]) {
        if (tableView == _tableView) {
            static NSString *cellId = @"MenuListCellId";
            MenuListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"MenuListCell" owner:self options:nil] lastObject];
            }
            if (indexPath.row == currentSub) {
                cell.nameLabel.textColor = [UIColor colorFromHexCode:@"#ff7200"];
                cell.backgroundColor = [UIColor whiteColor];
            }else {
                cell.nameLabel.textColor = [UIColor blackColor];
                cell.backgroundColor = [UIColor colorFromHexCode:@"#e4e4e4"];
            }
            cell.nameLabel.text = self.numOneArr[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else if (tableView == _subTableView) {
            static NSString *cellId = @"MenuListCellId";
            MenuListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"MenuListCell" owner:self options:nil] lastObject];
            }
            UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 44.5, self.frame.size.width, 0.5)];
            l1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
            
            
            NSArray *arr = [_numListArr objectAtIndex:currentSub];
            NSLog(@"---->%ld",indexPath.row);
            [cell addSubview:l1];

            if (indexPath.row < [arr count]) {
                cell.nameLabel.text  = [arr objectAtIndex:indexPath.row];
            }
//            cell.nameLabel.text = [arr objectAtIndex:indexPath.row];
            if (indexPath.row == c3 && currentSub == c2) {
                cell.nameLabel.textColor = [UIColor colorFromHexCode:@"#ff7200"];
                cell.imagename.image = [UIImage imageNamed:@"pub_duihao"];

            }else {
                cell.nameLabel.textColor = [UIColor blackColor];
                cell.imagename.image = [UIImage imageNamed:@""];

            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    static NSString *cellId = @"MenuListCellId";
    MenuListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MenuListCell" owner:self options:nil] lastObject];
    }
    UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 44.5, self.frame.size.width, 0.5)];
    l1.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [cell addSubview:l1];
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, 20)];
    cell.nameLabel.text = self.numArr[indexPath.row];
    if (indexPath.row == current) {
        cell.nameLabel.textColor = [UIColor colorFromHexCode:@"#ff7200"];
        cell.imagename.image = [UIImage imageNamed:@"pub_duihao"];
    }else {
        cell.nameLabel.textColor = [UIColor blackColor];
        cell.imagename.image = [UIImage imageNamed:@""];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([_nameSyting isEqualToString:@"1"]) {
        current = indexPath.row;
        if (_touchBlock) {
            NSString *str = [NSString stringWithFormat:@"%ld",indexPath.row];
            _touchBlock(str,@"",@"");
        }
        [_tableView reloadData];
    }else {
        if (tableView == _tableView) {
            currentSub = indexPath.row;
            [_tableView reloadData];
            [_subTableView reloadData];
        }else if (tableView == _subTableView) {
            currentSubOne = indexPath.row;
            if (_touchBlock) {
                NSArray *arr = [_numListArr objectAtIndex:currentSub];
                NSString *strTitle = [NSString stringWithFormat:@"%@|%@",[self.numOneArr objectAtIndex:currentSub],[arr objectAtIndex:currentSubOne]];
                NSString *s1 = [NSString stringWithFormat:@"%ld",currentSub];
                NSString *str = [NSString stringWithFormat:@"%ld",currentSubOne];
                _touchBlock(strTitle,s1,str);
            }
            [_subTableView reloadData];
            [_tableView reloadData];
        }
    }
}

//- (UITableViewCellAccessoryType)tableView:(UITableView*)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath*)indexPath {
//    if(current==indexPath.row) {
//        _titleLabel.textColor = [UIColor orangeColor];
//        return UITableViewCellAccessoryCheckmark;
//    } else {
//        _titleLabel.textColor = [UIColor blackColor];
//        return UITableViewCellAccessoryNone;
//    }
//}

@end
